from fastapi import FastAPI
import uvicorn

# 创建app对象
app = FastAPI()

# 测试数据
books = [
    {"id": 1, "title": "看漫画学Python", "author": "关东升", "price": 29.99},
    {"id": 2, "title": "杀死一只知更鸟", "author": "Harper Lee", "price": 19.99},
    {"id": 3, "title": "Python从小白到大牛", "author": "关东升", "price": 49.99},
    {"id": 4, "title": "漫画Java", "author": "关东升", "price": 39.99},
]


# 路由函数
@app.get("/books/{book_id}")
async def read_book(book_id: int):
    # 调用数据查询数据
    for book in books:
        # 遍历books
        if book["id"] == book_id:
            # 这就是我们需要的数据
            return {"book": book}
    # 没有找到合适的数据
    return {"error": "图书没有找到"}


if __name__ == "__main__":
    uvicorn.run("main:app", host="127.0.0.1", port=8000, reload=True)
